<template>
	<view class="comment-box">
		<view class="comment-header">
			<view class="comment-header__logo">
				<image :src="comments.author.avatar" mode="aspectFill"></image>
			</view>
			<view class="comment-header__info">
				<view class="title" v-if="!comments.is_reply">
					{{comments.author.author_name}}
				</view>
				<view class="title" v-else>
					{{comments.author.author_name}}
					<text class="reply-text">回复</text>{{comments.to}}
				</view>
				<view>
					{{comments.create_time | formatTime}}
				</view>
			</view>
		</view>
		<view class="comment-content">
			<view>{{comments.comment_content}}</view>
			<view class="comment-info">
				<view class="comment-button" @click="commentsReply({comments, is_reply:reply})">回复</view>
			</view>
			<view class="comment-reply" v-for="item in comments.reply" :key="item.comment_id">
				<comment-box :reply="true" :comments="item" @reply="commentsReply"></comment-box>
			</view>
		</view>
		
	</view>
</template>

<script>
	import commentBox from '@/components/comment-box/comment-box.vue'
	import {parseTime} from '@/utils/index.js'
	export default {
		name:"comment-box",
		components: {
			commentBox
		},
		props: {
			comments: {
				type: Object,
				default() {
					return {}
				}
			},
			reply: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				
			};
		},
		filters: {
			formatTime(time) {
				return parseTime(time)
			}
		},
		methods: {
			commentsReply(comment) {
				// 判断是 主回复 还是 子回复
				if(comment.is_reply) {
					// 子回复
					comment.comments.reply_id = comment.comments.comment_id
					comment.comments.comment_id = this.comments.comment_id
				}
				// console.log(comment);
				this.$emit('reply',comment)
			}
		}
	}
</script>

<style lang="scss">
	.comment-box {
		margin: 15px 0;
		.comment-header {
			display: flex;
			.comment-header__logo {
				flex-shrink: 0;
				width: 35px;
				height: 35px;
				border-radius: 50%;
				overflow: hidden;
				image {
					width: 100%;
					height: 100%;
				}
			}
			.comment-header__info {
				padding-left: 15px;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				font-size: 12px;
				color: #999;
				line-height: 1;
				.title {
					margin-bottom: 10px;
					font-size: 14px;
					color: #333;
					.reply-text {
						margin: 0 10px;
						font-weight: bold;
						color: #000;
					}
				}
			}
		}
		.comment-content {
			margin-top: 10px;
			font-size: 14px;
			color: #000;
			.comment-info {
				margin-top: 10px;
				display: flex;
				.comment-button {
					padding: 2px 10px;
					border: 1px #ccc solid;
					border-radius: 5px;
					font-size: 12px;
					color: #999;
				}
			}
			.comment-reply {
				display: flex;
				margin: 15px 0;
				padding: 0 10px;
				border: 1px #eee solid;
			}
		}
	}
</style>
